package blend

import (
	"image"
	"image/color"
	"math"
)

func BlendSDFToAlpha(pre [][]float64, cur [][]float64, length, width, idx, photoNum int, outputImg *image.RGBA) {
	for i := 0; i < width; i++ {
		for j := 0; j < length; j++ {
			_min := 1 - float64(idx)/float64(photoNum-1)
			preValue := pre[i][j]
			curValue := cur[i][j]
			if preValue*curValue <= 0 {
				alpha := curValue/(math.Abs(preValue)+curValue)/float64(photoNum-1) + _min
				outputImg.Set(j, i, GetColor(255, 255, 255, uint8(alpha*255)))
			} else if curValue < 0 && preValue < 0 {
				outputImg.Set(j, i, GetColor(255, 255, 255, 0))
			}
		}
	}
}

func BlendSDFToGrey(pre [][]float64, cur [][]float64, length, width, idx, photoNum int, outputImg *image.RGBA) {
	for i := 0; i < width; i++ {
		for j := 0; j < length; j++ {
			_min := 1 - float64(idx)/float64(photoNum-1)
			preValue := pre[i][j]
			curValue := cur[i][j]
			if preValue*curValue <= 0 {
				grey := (curValue/(math.Abs(preValue)+curValue)/float64(photoNum-1) + _min) * 255
				outputImg.Set(j, i, GetColor(uint8(grey), uint8(grey), uint8(grey), 255))
			} else if curValue < 0 && preValue < 0 {
				outputImg.Set(j, i, GetColor(0, 0, 0, 255))
			}
		}
	}
}

func GetColor(r, g, b, a uint8) color.NRGBA {
	return color.NRGBA{R: r, G: g, B: b, A: a}
}
